package com.ruoyi.material.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.material.vo.MaterialVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.material.domain.Materials;
import com.ruoyi.material.service.IMaterialsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 物料管理Controller
 *
 * @author shanhai
 * @date 2024-09-12
 */
@RestController
@RequestMapping("/materials/materials")
public class MaterialsController extends BaseController {
    // 定义类变量
    public static List<MaterialVo> list = new ArrayList<>();

    @Autowired
    private IMaterialsService materialsService;

    /**
     * 屎山别看 ！！！！！！！！！！！！！！
     *
     */
    @PostMapping("/setList")
    public void setList(@RequestBody List<MaterialVo> materialVoList) {
        this.list = materialVoList;
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<MaterialVo> util = new ExcelUtil<MaterialVo>(MaterialVo.class);
        // 构造样例数据 添加2行示例数据
        List<MaterialVo> materialVoList = new ArrayList<>();
        materialVoList.add(new MaterialVo("BM3451SMDC(TSSOP-28)", new BigDecimal(1), "比亚迪充电平衡芯片", 1L, "当前数据为样例数据，请注意删除！！！"));
        materialVoList.add(new MaterialVo("SI2306(SOT-23)", new BigDecimal(36), "N-MOS", 1L, "当前数据为样例数据，请注意删除！！！"));
        util.exportExcel(response, materialVoList, "物料数据");
    }

    /**
     * 依托答辩  答辩答辩
     *
     * @param response
     */
    @PostMapping("/importResult")
    public void importResult(HttpServletResponse response) {
        ExcelUtil<MaterialVo> util = new ExcelUtil<>(MaterialVo.class);
        List<MaterialVo> materialVoList = this.list;
        util.exportExcel(response, materialVoList, "物料数据");
        // 初始化 List
        list = new ArrayList<>();
    }


    @Log(title = "物料管理", businessType = BusinessType.EXPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        // 解析上传的 excel数据  从上传文件中读取Excel数据并解析为MaterialVo对象列表。
        ExcelUtil<MaterialVo> util = new ExcelUtil<>(MaterialVo.class);
        List<MaterialVo> materialVoList = util.importExcel(file.getInputStream());
        // 处理数据 获取list 处理后的 结果
        List<MaterialVo> list = materialsService.dealData(materialVoList);
        return AjaxResult.success(list);
    }

    /**
     * 导出物料管理列表
     */
    @PreAuthorize("@ss.hasPermi('materials:materials:export')")
    @Log(title = "物料管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Materials materials) {
        List<Materials> list = materialsService.selectMaterialsList(materials);
        ExcelUtil<Materials> util = new ExcelUtil<Materials>(Materials.class);
        util.exportExcel(response, list, "物料管理数据");
    }

    /**
     * 查询物料管理列表
     */
    @PreAuthorize("@ss.hasPermi('materials:materials:list')")
    @GetMapping("/list")
    public TableDataInfo list(Materials materials) {
        startPage();
        List<Materials> list = materialsService.selectMaterialsList(materials);
        return getDataTable(list);
    }


    /**
     * 获取物料管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('materials:materials:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(materialsService.selectMaterialsById(id));
    }

    /**
     * 新增物料管理
     */
    @PreAuthorize("@ss.hasPermi('materials:materials:add')")
    @Log(title = "物料管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Materials materials) {
        return toAjax(materialsService.insertMaterials(materials));
    }

    /**
     * 修改物料管理
     */
    @PreAuthorize("@ss.hasPermi('materials:materials:edit')")
    @Log(title = "物料管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Materials materials) {
        return toAjax(materialsService.updateMaterials(materials));
    }

    /**
     * 删除物料管理
     */
    @PreAuthorize("@ss.hasPermi('materials:materials:remove')")
    @Log(title = "物料管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(materialsService.deleteMaterialsByIds(ids));
    }
}
